﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using DAO_DTO_WS.DAO;
using DAO_DTO_WS.DTO;

namespace LayoutTemplate
{
    public partial class XemChiTietSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id;
                try
                {
                    id = Request.QueryString["id"];
                }
                catch
                {
                    Response.Redirect("XemDanhSachSanPham.aspx?MaDiaPhuong=1");
                    return;
                }
                string a = Session["ID"].ToString();
                if (Session["ID"].ToString() == "")
                {
                    btnBinhLuan.Enabled = false;
                    txtBinhLuan.Enabled = false;
                }
                if (IsPostBack == false)
                {
                    if (id == null)
                    {
                        Response.Redirect("XemDanhSachSanPham.aspx?MaDiaPhuong=1");
                    }
                    else
                    {
                        SanPhamDTO temp = SanPhamDAO.laySPTheoMa(int.Parse(id));
                        temp.SoLanXem += 1;
                        SanPhamDAO.CapNhatSanPham(temp);
                        hlXemTatCaBinhLuan.NavigateUrl = "XemTatCaLoiBinh.aspx?id=" + id.ToString();
                        string maSanPham = Convert.ToString(id);
                        SanPhamDTO sp = new SanPhamDTO();
                        try
                        {
                            sp = SanPhamDAO.laySPTheoMa(int.Parse(maSanPham));
                        }
                        catch
                        {
                            Response.Redirect("XemDanhSachSanPham.aspx?MaDiaPhuong=1");
                            return;
                        }
                        lbTenSanPham.Text = sp.TieuDe;
                        lbGiaBanDau.Text = sp.GiaGoc.ToString();
                        lbGiaBanGiam.Text = sp.GiaTietKiem.ToString();
                        lbMaSanPham.Text = maSanPham;
                        lbSoLuongToiDa.Text = sp.SoLuongToiDa.ToString();
                        lbSoLuongToiThieu.Text = sp.SoLuongToiThieu.ToString();
                        lbThongTinSanPham.Text = sp.ThongTin;
                        lbSlg.Text = sp.SoLuong.ToString();
                        lbThoiGianBatDau.Text = sp.ThoiGianBatDau.ToShortDateString();
                        lbThoiGianKetThuc.Text = sp.ThoiGianKetThuc.ToShortDateString();
                        lbSoLuotXem.Text = (sp.SoLanXem/2).ToString();
                        //code them lay danh sach doanh nghiep
                        LinkButton1.Text = DoanhNghiepDAO.LayDNTheoMa(sp.MaDoanhNghiep).TenDoanhNghiep;
                    }
                }
            }
        }

        protected void ibtnMuaSanPham_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["ID"] == null || Session["ID"].ToString() == "")
            {
                Response.Redirect("TrangLoi.aspx?Error=2");
            }
            else
            {
                int soluongmua = 0;
                try
                {
                    soluongmua = int.Parse(tbSoLuong.Text.Trim());
                }
                catch
                {
                    Response.Redirect("TrangLoi.aspx?Error=7");
                }
                int masp=int.Parse (Request.QueryString["id"]);
                SanPhamDTO sanpham = SanPhamDAO.laySPTheoMa(masp);
                int soluongton = sanpham.SoLuongToiDa - sanpham.SoLuong;
                if (soluongmua > soluongton)
                {
                    Response.Redirect("TrangLoi.aspx?Error=6");
                }
                else
                {
                    sanpham.SoLuong += soluongmua;
                    if (sanpham.SoLuong == sanpham.SoLuongToiDa)
                        sanpham.TinhTrang = 3;
                    DonHangDTO donhang = new DonHangDTO();
                    donhang.MaSanPham= masp;
                    donhang.MaThanhVien=(int)Session["ID"];
                    donhang.NgayMua= DateTime.Now;
                    donhang.SoLuong=soluongmua;
                    donhang.TongTien = soluongmua * sanpham.GiaTietKiem;
                    donhang.MaLoaiThanhToan = 1;
                    donhang.TinhTrang = 1;
                    bool test=DonHangDAO.ThemDonHang(donhang);
                    test=test&SanPhamDAO.CapNhatSanPham(sanpham);
                    if (test)
                        Response.Redirect("XemDanhSachSanPham.aspx");

                }
            }
        }

        protected void BinhLuan_TextChanged(object sender, EventArgs e)
        {
            if (txtBinhLuan.Text.Trim() != "")
                btnBinhLuan.Enabled = true;
        }

        protected void btnBinhLuan_Click(object sender, EventArgs e)
        {
            int maThanhVien, maSanPham;
            string binhLuan = txtBinhLuan.Text.Trim();
            try
            {
                maThanhVien = (int)Session["ID"];
                maSanPham = int.Parse(Request.QueryString["id"]);
            }
            catch
            {
                Response.Redirect("DanhSachSanPham?MaDiaPhuong=1.aspx");
                return;
            }

            LoiBinhDAO.ThemLoiBinh(maThanhVien, maSanPham, binhLuan);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            int masapham = int.Parse(Request.QueryString["id"]);
            Application["Back"] = Request.RawUrl;
            Response.Redirect("ThongTinDoanhNghiep.aspx?id=" + SanPhamDAO.laySPTheoMa(masapham).MaDoanhNghiep.ToString());
        }
    }
}
